<template>
    <div>
        <h1>课程管理</h1>
        <!-- 搜索框 -->
        <div style="margin: 10px 0;">
            <el-input v-model="nameSearch" placeholder="请输入课程名称" style="width: 220px;" prefix-icon="el-icon-search"
                clearable></el-input>
            <el-button type="primary" style="margin-left: 5px;" @click="load">搜索</el-button>
            <el-button style="margin-left: 5px;" @click="resetInput">重置</el-button>
        </div>
        <!-- 功能按钮 -->
        <div style="margin: 10px 0;">
            <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
            <el-popconfirm style="margin-left: 10px;" confirm-button-text='好的' cancel-button-text='不用了'
                icon="el-icon-info" icon-color="red" title="您确定批量删除这些数据吗？" @confirm="handleDelBatch">
                <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
        </div>
        <!-- 表格 -->
        <el-table :data="tableData" border stripe height="440" header-cell-style="background: #F3F3F3;"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column prop="id" label="ID" width="70" fixed="left" sortable>
            </el-table-column>
            <el-table-column prop="name" label="课程名称">
            </el-table-column>
            <el-table-column prop="no" label="课程编号">
            </el-table-column>
            <el-table-column prop="teacher" label="授课老师">
            </el-table-column>
            <el-table-column prop="type" label="必/选修">
            </el-table-column>
            <el-table-column label="操作" width="180" fixed="right">
                <template slot-scope="scope">
                    <el-button type="warning" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i>
                    </el-button>
                    <el-popconfirm style="margin-left: 8px;" confirm-button-text='好的' cancel-button-text='不用了'
                        icon="el-icon-info" icon-color="red" title="您确定删除吗？" @confirm="handleDelete(scope.row.id)">
                        <el-button type="danger" slot="reference">删除 <i class="el-icon-delete"></i>
                        </el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <div style="padding: 10px 0;">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
                :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
        <!-- 新增or编辑 对话框 -->
        <el-dialog title="课程信息" :visible.sync="dialogFormVisible" width="30%" top="10px">
            <el-form label-width="80px">
                <el-form-item label="课程名称" width="50px">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="课程编号">
                    <el-input v-model="form.no" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="授课老师">
                    <el-input v-model="form.teacher" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="教室">
                    <el-input v-model="form.classroom" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="必/选修">
                    <el-radio v-model="form.type" label="必修">必修</el-radio>
                    <el-radio v-model="form.type" label="选修">选修</el-radio>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "Student",
        data() {
            return {
                tableData: [],
                total: 0,
                pageNum: 1,
                pageSize: 10,
                nameSearch: '',
                dialogFormVisible: false,
                form: {},
                multipleSelection: [],
                roles: [],
            }
        },
        created() {
            this.load()
        },
        methods: {
            load() {
                this.request.get("/course/page", {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        name: this.nameSearch,
                        role: 'ROLE_USER'
                    }
                }).then(res => {
                    console.log(res)
                    this.tableData = res.data.records
                    this.total = res.data.total
                })

                this.request.get("/role").then(res => {
                    this.roles = res.data
                })
            },
            // 获取每页显示几条的数据
            handleSizeChange(pageSize) {
                // console.log(pageSize)
                this.pageSize = pageSize
                this.load()
            },
            // 获取第某个页从第几个开始显示的数据
            handleCurrentChange(pageNum) {
                // console.log(pageNum);
                this.pageNum = pageNum
                this.load()
            },
            // 搜索框重置按钮
            resetInput() {
                this.nameSearch = ""
                this.load()
            },
            // 新增按钮
            handleAdd() {
                this.dialogFormVisible = true
                this.form = {}
            },
            // 对话框的确认按钮
            save() {
                this.request.post("/course", this.form).then(res => {
                    if (res.code === '200') {
                        this.$message.success("操作成功")
                        this.dialogFormVisible = false
                        this.load()
                    } else {
                        this.$message.error("操作失败")
                    }
                })
            },
            // 编辑按钮
            handleEdit(row) {
                this.form = row
                this.dialogFormVisible = true
            },
            // 删除按钮
            handleDelete(id) {
                this.request.delete("/course/" + id).then(res => {
                    if (res.code === '200') {
                        this.$message.success("删除成功")
                        this.load()
                    } else {
                        this.$message.error("删除失败")
                    }
                })
            },
            // 表格批量选择
            handleSelectionChange(val) {
                console.log(val)
                this.multipleSelection = val
            },
            // 批量删除按钮
            handleDelBatch() {
                let ids = this.multipleSelection.map(v => v.id) // {{}, {}, {}} => [1,2,3]
                this.request.post("/course/del/batch", ids).then(res => {
                    if (res.code === '200') {
                        this.$message.success("批量删除成功")
                        this.load()
                    } else {
                        this.$message.error("批量删除失败")
                    }
                })
            },
        }
    }
</script>